package org.jash.learnmvp.video;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import org.jash.learnmvp.R;
import org.jash.learnmvp.entity.VideoEntity;
import org.jash.learnmvp.mvp.BaseFragment;

import java.util.List;


public class VideoFragment extends BaseFragment<ConStruct.IVideoView, VideoPresenter> implements ConStruct.IVideoView {
    private RecyclerView recycler;
    private VideoAdapter adapter;
    @Override
    protected int layoutId() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initData() {
        presenter = new VideoPresenter();
        presenter.loadAllVideo();
    }

    @Override
    protected void initView() {
        recycler = getView().findViewById(R.id.recycler);
        adapter = new VideoAdapter();
        adapter.setListener(bean -> {
            requireContext().startActivity(new Intent(requireContext(), VideoPlayerActivity.class) {{
                putExtra("url", bean.getVideourl());
            }});
        });
        recycler.setAdapter(adapter);
    }

    @Override
    public void videoSuccess(List<VideoEntity.DataBean> list) {
        adapter.addAll(list);
    }

    @Override
    public void videoFailure(Throwable t) {
        Toast.makeText(requireContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        Log.e("TAG", "videoFailure: ", t);
    }
}